import { defineStore } from 'pinia';
import * as allLocalKey from '@/constants/dict/localDict';
import * as allRemoteKey from '@/constants/dict/remoteDict';

import { DictItem } from '@/types/global';
import { Keys } from './types';

const useDictStore = defineStore('keys', {
  state: (): Keys => ({
    dictMap: {},
  }),

  getters: {
    keys(state: Keys): Keys {
      return { ...state };
    },
  },

  actions: {
    async getKeys() {
      // 远程词典
      const remoteRequestMap: any = allRemoteKey;
      const keyArr = Object.keys(remoteRequestMap);
      keyArr.forEach((t) => {
        remoteRequestMap[t]().then((res: Record<string, DictItem[]>) => {
          Object.keys(res).forEach((m) => {
            this.dictMap[m] = res[m];
          });
        });
      });

      // 本地词典
      const dictMap: any = {};
      // 加载本地词典 本地词典可以是map 可以是数组
      Object.keys(allLocalKey).forEach((t) => {
        const item: any = (allLocalKey as any)[t];
        dictMap[t] = item;
      });

      this.dictMap = dictMap;
    },
  },
});

export default useDictStore;
